package cn.senjoeson.triper.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.senjoeson.mylibrary.utilschanged.GlideUtils;
import cn.senjoeson.triper.R;
import cn.senjoeson.triper.activity.DestinationActivity;
import cn.senjoeson.triper.bean.FavoriteBean;

/**
 * @author Android
 * @date 2018/1/8
 */

public class FavoriteListAdapter extends RecyclerView.Adapter<FavoriteListAdapter.ViewHolder> {

    private List<FavoriteBean> mPlaceBeans;
    private OnItemClickListener mOnItemClickListener;
    private Context mContext;

    public FavoriteListAdapter(Context context, List<FavoriteBean> placeBeans) {
        mPlaceBeans = placeBeans;
        this.mContext=context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.favorite_location_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final FavoriteBean favoriteBean = mPlaceBeans.get(position);
        holder.mLocationName.setText(favoriteBean.getPlaceName());
        GlideUtils.loadImage(holder.itemView.getContext(),favoriteBean.getImageUrl(),holder.mLocatonImage);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               /* if (mOnItemClickListener!=null)
                mOnItemClickListener.onItemClick(v, holder.getAdapterPosition());*/
                Intent intent = new Intent(mContext, DestinationActivity.class);
                intent.putExtra("placeId",favoriteBean.getPlaceId());
                mContext.startActivity(intent);
            }

        });
    }


    @Override
    public int getItemCount() {
        return mPlaceBeans == null ? 0 : mPlaceBeans.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        if (onItemClickListener != null) {
            this.mOnItemClickListener = onItemClickListener;

        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.locatonImage)
        ImageView mLocatonImage;
        @BindView(R.id.locationName)
        TextView mLocationName;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }
}
